package com.lujianfei.redpacketstat.manager

import android.os.Build
import androidx.annotation.RequiresApi
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


/**
 * Author: lujianfei
 * Date: 2024/12/23 11:19
 * Description:事件管理类
 */

class MessageManager private constructor() {
    companion object {
        val instance: MessageManager by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            MessageManager()
        }
    }

    private val listeners = mutableSetOf<NotifyListener>()
    private val scope = CoroutineScope(Dispatchers.Main)

    fun addListener(listener: NotifyListener) {
        listeners.add(listener)
    }

    fun removeListener(listener: NotifyListener) {
        listeners.remove(listener)
    }

    fun clear() {
        listeners.clear()
    }

    fun notify(unHandleCount:Int, handleCount:Int) {
        scope.launch {
            listeners.forEach {
                it.onTabDataChange(unHandleCount, handleCount)
            }
        }
    }
}


interface NotifyListener {
    fun onTabDataChange(unHandleCount:Int, handleCount:Int)
}